package tech.aistar.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.aistar.dto.Result;
import tech.aistar.entity.User;
import tech.aistar.service.impl.LevelServiceImpl;
import tech.aistar.service.impl.UserServiceImpl;

import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/login")
public class     LoginAction {
    @Autowired
    private UserServiceImpl userServiceimpl;
    @Autowired
    private LevelServiceImpl levelService;
    @RequestMapping("/load")
    public String onload(){
        return "login";
    }
    @RequestMapping("/uname")
    public @ResponseBody
    Result checkuname(HttpServletRequest request){
        String username = request.getParameter("uname");

        if (null==userServiceimpl.findUserBynickname(username)){
            return new Result("301","用户名可用");

        }else{
            return new Result("302","用户名已存在");
        }
    }

    @RequestMapping("/sms")
    public @ResponseBody
    Result SendSms(HttpServletRequest request){
        String phone = request.getParameter("phone");
        System.out.println(phone);
        String code = userServiceimpl.SendMessage(phone);
        System.out.println(code);
        return new Result("401","发送成功",code);
    }


    @RequestMapping("/register")
    public @ResponseBody
    Result Register(User user){

        int count = userServiceimpl.register(user);
        User user1 = userServiceimpl.findUserBynickname(user.getUsNickname());
        System.out.println(user1);
        levelService.insertUserlevel(String.valueOf(user1.getUsId()));
        if (count>0){
            onload();
            return new Result("100","注册成功");
        }else{
            return new Result("101","注册失败");
        }
    }
    @RequestMapping("/log")
    public @ResponseBody
    Result loging(HttpServletRequest request){
        String username = request.getParameter("uname");
        User user1 = userServiceimpl.findUserBynickname(username);
        if (user1==null){
            return new Result("02","不存在");
        }else{
            return new Result("01","密码",user1.getUsPassword());
        }
    }
    @RequestMapping("loginout")
    public @ResponseBody Result Loginout(HttpSession session){
        session.removeAttribute("user");
        return null;
    }



}

